/*
 *   Copyright (C) 2006-2007 Jiri Majer. All Rights Reserved.
 *   DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 *   This code is free software; you can redistribute it and/or modify it
 *   under the terms of the GNU General Public License version 2 only, as
 *   published by the Free Software Foundation.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */



package org.diabetesdiary.simulator;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

/**
 *
 * @author  Jiri Majer
 */
public class NewJFrame extends javax.swing.JFrame {
    
    private Date beginDate;
    private Date endDate;
    private float weight;
    
    
    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
        weight = 70f;
        SimulationManager insMon = new SimulationManager(0.2f,0.2f,weight);
        Calendar cal = Calendar.getInstance();
        
        cal.add(Calendar.DAY_OF_MONTH,2);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        beginDate = cal.getTime();
        cal.add(Calendar.DAY_OF_MONTH,1);
        endDate = cal.getTime();
        
        cal.add(Calendar.DAY_OF_MONTH,-3);
        
        for(int i=0; i<4;i++){
            cal.set(Calendar.HOUR_OF_DAY,7);
            cal.set(Calendar.MINUTE,45);
            insMon.addInsulinInjection(InsulinParam.REGULAR,3,cal.getTime());
            insMon.addInsulinInjection(InsulinParam.LENTE,12,cal.getTime());
            
            cal.set(Calendar.HOUR_OF_DAY,17);
            cal.set(Calendar.MINUTE,30);
            insMon.addInsulinInjection(InsulinParam.REGULAR,4,cal.getTime());
            
            
            cal.set(Calendar.HOUR_OF_DAY,22);
            cal.set(Calendar.MINUTE,30);
            insMon.addInsulinInjection(InsulinParam.LENTE,18,cal.getTime());
            
            
            cal.set(Calendar.HOUR_OF_DAY,8);
            cal.set(Calendar.MINUTE,0);
            insMon.addCarbohydrateIntake(30,cal.getTime());
            
            cal.set(Calendar.HOUR_OF_DAY,10);
            cal.set(Calendar.MINUTE,0);
            insMon.addCarbohydrateIntake(20,cal.getTime());
            
            cal.set(Calendar.HOUR_OF_DAY,12);
            cal.set(Calendar.MINUTE,0);
            insMon.addCarbohydrateIntake(40,cal.getTime());
            
            cal.set(Calendar.HOUR_OF_DAY,15);
            cal.set(Calendar.MINUTE,50);
            insMon.addCarbohydrateIntake(10,cal.getTime());
            
            cal.set(Calendar.HOUR_OF_DAY,18);
            cal.set(Calendar.MINUTE,0);
            insMon.addCarbohydrateIntake(30,cal.getTime());
            
            cal.add(Calendar.DAY_OF_MONTH,1);
        }
        
        jComboBox1.setModel(createGraphModel());
        jComboBox1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(jComboBox1.getSelectedItem() != null){
                    SREnum en = (SREnum)jComboBox1.getSelectedItem();
                    ChartPanel panel1 = createChart(createSeries(en,en.name(),res),en.name());
                    jScrollPane1.setViewportView(panel1);
                }
            }
        });
        
        
        res = insMon.getSimulationResult();       
        
    }
    
    public ComboBoxModel createGraphModel(){
        DefaultComboBoxModel model = new DefaultComboBoxModel(SREnum.values());
        model.insertElementAt(null,0);
        model.setSelectedItem(null);
        return model;
    }
    
    private SimulationResult res;
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1072, Short.MAX_VALUE)
                    .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 200, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    
    public TimeSeries createSeries(SREnum sre, String name, SimulationResult profile){
        TimeSeries series = new TimeSeries(sre.name(),Millisecond.class);
        for(Date date : profile.getProfile().keySet()){
            if(date.after(beginDate) && date.before(endDate)){
                double value = 0;
                switch(sre){
                    case ACTIVE_INSULIN: value = profile.getProfile().get(date).getActiveInsulin();
                    break;
                    case EQ_INSULIN: value = profile.getProfile().get(date).getEqInsulin();
                    break;
                    case GLUCOSE: value = profile.getProfile().get(date).getGlucose();
                    break;
                    case GLUCOSE_GUT: value = profile.getProfile().get(date).getGlucoseInGut();
                    break;
                    case GLUCOSE_UTIL: value = profile.getProfile().get(date).getGlucoseUtilisation();
                    break;
                    case NHGB: value = profile.getProfile().get(date).getNhgb();
                    break;
                    case PLASMA_INSULIN: value = profile.getProfile().get(date).getPlasmaInsulin();
                    break;
                    case RENAL_EXCRETION: value = profile.getProfile().get(date).getRenalGlucoseExcretion();
                    break;
                }
                series.add(new Millisecond(date),value);
            }
        }
        return series;
    }
    
    public ChartPanel createChart(TimeSeries series, String name){
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(series);
        JFreeChart chart = ChartFactory.createTimeSeriesChart(null,"Date","Blood glucose",dataset,true,true,false);
        ChartPanel panel = new ChartPanel(chart);
        panel.setMouseZoomable(true, false);
        panel.setPreferredSize(new Dimension(800,100));
        chart.setAntiAlias(false);
        return panel;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
}
